package com.qdlc.p2p.biz.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.qdlc.p2p.biz.service.UserCertificationApplyService;
import com.qdlc.p2p.common.util.DateUtil;
import com.qdlc.p2p.common.util.StringUtil;
import com.qdlc.p2p.dal.common.model.jpa.OrderFilter.OrderType;
import com.qdlc.p2p.dal.common.model.jpa.PageDataList;
import com.qdlc.p2p.dal.common.model.jpa.QueryParam;
import com.qdlc.p2p.dal.common.model.jpa.SearchFilter;
import com.qdlc.p2p.dal.common.model.jpa.SearchFilter.Operators;
import com.qdlc.p2p.dal.dao.UserBaseInfoDao;
import com.qdlc.p2p.dal.dao.UserCertificationApplyDao;
import com.qdlc.p2p.dal.dao.UserCertificationDao;
import com.qdlc.p2p.dal.dto.CertificationType;
import com.qdlc.p2p.dal.dto.User;
import com.qdlc.p2p.dal.dto.UserBaseInfo;
import com.qdlc.p2p.dal.dto.UserCertification;
import com.qdlc.p2p.dal.dto.UserCertificationApply;
import com.qdlc.p2p.dal.model.UserCertificationApplyModel;

@Service("userCertificationApplyService")
public class UserCertificationApplyServiceImpl implements UserCertificationApplyService {

    @Resource
    private UserCertificationApplyDao userCertificationApplyDao;

    @Resource
    private UserCertificationDao userCertificationDao;

    @Resource
    private UserBaseInfoDao userBaseInfoDao;

    @Override
    public void add(UserCertificationApply userCertificationApply) {
        userCertificationApplyDao.save(userCertificationApply);
    }

    @Override
    public PageDataList<UserCertificationApplyModel> applyList(
            UserCertificationApplyModel model) {
        QueryParam param = QueryParam.getInstance();
        //模糊查询条件 start
        if (StringUtil.isNotBlank(model.getSearchName())) {
            SearchFilter orFilter1 = new SearchFilter("user.userName", Operators.LIKE, model.getSearchName().trim());
            SearchFilter orFilter2 = new SearchFilter("user.realName", Operators.LIKE, model.getSearchName().trim());
            param.addOrFilter(orFilter1, orFilter2);
        }
        //模糊查询条件 end
        //精确查询条件 start
        if (StringUtil.isNotBlank(model.getUserName())) {
            param.addParam("user.userName", Operators.LIKE, model.getUserName().trim());
        }
        if (StringUtil.isNotBlank(model.getRealName())) {
            param.addParam("user.realName", Operators.LIKE, model.getRealName().trim());
        }
        if (!StringUtil.isBlank(model.getStartTime())) {
            param.addParam("addTime", Operators.GT, DateUtil.valueOf(model.getStartTime()));
        }
        if (!StringUtil.isBlank(model.getEndTime())) {
            Date date = DateUtil.valueOf(model.getEndTime());
            param.addParam("addTime", Operators.LT, DateUtil.rollDay(date, 1));
        }
        if (model.getStatus() != 99) {
            param.addParam("status", model.getStatus());
        }
        param.addOrder(OrderType.DESC, "id");
        //精确查询条件 end
        param.addPage(model.getPage(), model.getRows());
        PageDataList<UserCertificationApply> pList = userCertificationApplyDao.findPageList(param);
        PageDataList<UserCertificationApplyModel> modelList = new PageDataList<UserCertificationApplyModel>();
        List<UserCertificationApplyModel> list = new ArrayList<UserCertificationApplyModel>();
        modelList.setPage(pList.getPage());
        for (UserCertificationApply apply : pList.getList()) {
            UserCertificationApplyModel ucm = UserCertificationApplyModel
                    .instance(apply);
            User user = apply.getUser();
            UserBaseInfo userInfo = userBaseInfoDao.findByUserId(user.getUserId());
            if (userInfo != null) {
                ucm.setBirthday(userInfo.getBirthday());
                ucm.setEducation(userInfo.getEducation());
                ucm.setMaritalStatus(userInfo.getMaritalStatus());
                ucm.setProvince(userInfo.getProvince());
                ucm.setCity(userInfo.getCity());
                ucm.setWorkExperience(userInfo.getWorkExperience());
                ucm.setMonthIncomeRange(userInfo.getMonthIncomeRange());
                ucm.setCarStatus(userInfo.getCarStatus());
                ucm.setHouseStatus(userInfo.getHouseStatus());
            }
            ucm.setRealName(user.getRealName());
            ucm.setUserName(user.getUserName());
            ucm.setUserId(user.getUserId());
            ucm.setTypeName(apply.getType().getName());
            ucm.setTypeId(apply.getType().getId());
            ucm.setUser(null);
            ucm.setType(null);
            list.add(ucm);
        }
        modelList.setList(list);
        return modelList;
    }

    @Override
    public void certificationVerify(UserCertificationApplyModel model) {
        UserCertificationApply apply = userCertificationApplyDao.find(model.getId());
        apply.setStatus(model.getStatus());
        apply.setVerifyRemark(model.getVerifyRemark());
        apply.setVerifyTime(DateUtil.getNow());
        apply.setOperator(model.getOperator());
        userCertificationApplyDao.merge(apply);
    }

    @Override
    public int count() {
        QueryParam param = QueryParam.getInstance().addParam("status", 0);
        return userCertificationApplyDao.countByCriteria(param);
    }

    @Override
    public int getStatusByUserAndTypeId(User user, long typeId) {
        return userCertificationApplyDao.getStatusByUserAndTypeId(user, typeId);
    }

    @Override
    public List<UserCertificationApplyModel> findByUser(User user) {
        QueryParam param = QueryParam.getInstance();
        param.addParam("user", user);
        param.addParam("status", 1);
        List<UserCertificationApply> list = userCertificationApplyDao.findByCriteria(param);
        List<UserCertificationApplyModel> models = new ArrayList<UserCertificationApplyModel>();
        for (UserCertificationApply ua : list) {
            UserCertificationApplyModel model = UserCertificationApplyModel.instance(ua);
            model.setTypeName(ua.getType().getName());
            model.setType(null);
            model.setUser(null);
            models.add(model);
        }
        return models;
    }

    @Override
    public void add(UserCertificationApply userCertificationApply,
                    List<UserCertification> list) {
        User user = userCertificationApply.getUser();
        CertificationType type = userCertificationApply.getType();
        UserCertificationApply userCertificationApply_ = null;
        if (user != null) {
            if (type != null) {
                userCertificationApply_ = userCertificationApplyDao.getUserCertificationApplyByUser(user, type);
            }
            if (userCertificationApply_ != null) {
                userCertificationApply_.setStatus(UserCertificationApplyModel.applyStatus);
                userCertificationApplyDao.update(userCertificationApply_);
            } else {
                userCertificationApplyDao.save(userCertificationApply);
            }
        }
        userCertificationDao.save(list);
    }
}
